package gov.pbc.nn.core.bdf.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bstek.bdf2.core.business.IDept;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.service.IDeptService;
import com.bstek.dorado.annotation.PropertyDef;

@Entity
@Table(name = "T_DEPT")
public class Dept implements IDept, java.io.Serializable {
	private static final long serialVersionUID = 1L;

	public static final String ROOT = "000000000";
	public static final String NANNING = "489145101";
	@Id
	@Column(name = "ID_", length = 60)
	@PropertyDef(label = "ID")
	private String id;
	@PropertyDef(label = "部门名称")
	@Column(name = "NAME_", length = 512)
	private String name;
	@PropertyDef(label = "备注")
	@Column(name = "DESC_", length = 120)
	private String desc;
	@PropertyDef(label = "公司ID")
	@Column(name = "COMPANY_ID_", length = 60)
	private String companyId;
	@PropertyDef(label = "创建日期")
	@Column(name = "CREATE_DATE_")
	private Date createDate;
	@PropertyDef(label = "上级ID")
	@Column(name = "PARENT_ID_", length = 60)
	private String parentId;
	@PropertyDef(label = "层级", description = "0：根；1：南宁；2：地市；3：县支行")
	@Column(name = "DEPT_LEVEL_")
	private String deptLevel;

	@PropertyDef(label = "级别", description = "0：南宁中支；1：南宁县支行；2：地市中支；3：地市县支行")
	@Transient
	private int level = -1;

	@Transient
	private IDept parent;
	@Transient
	private List<IUser> users;
	@Transient
	private List<IDept> children;

	@Override
	public String getCompanyId() {
		// TODO Auto-generated method stub
		return companyId;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public String getParentId() {
		// TODO Auto-generated method stub
		return parentId;
	}

	@Override
	public IDept getParent() {
		// TODO Auto-generated method stub
		return parent;
	}

	@Override
	public List<IUser> getUsers() {
		// TODO Auto-generated method stub
		return users;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public List<IDept> getChildren() {
		return children;
	}

	public void setChildren(List<IDept> children) {
		this.children = children;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public void setParent(IDept parent) {
		this.parent = parent;
	}

	public void setUsers(List<IUser> users) {
		this.users = users;
	}

	public String getDeptLevel() {
		return deptLevel;
	}

	public void setDeptLevel(String deptLevel) {
		this.deptLevel = deptLevel;
	}

	public int getLevel() {
		if (level > -1) {
			return level;
		}
		if (this.getId().equals(NANNING) || ROOT.equals(this.getId())) {
			// 南宁中支
			level = 0;
		} else {
			if (StringUtils.startsWith(this.getId(), Dept.NANNING)) {
				if ("3".equals(this.getDeptLevel())) {
					level = 1;
				} else {
					Dept dept = this;
					while (!Dept.ROOT.equals(dept.getParentId())) {
						dept = this.getParentDept(dept.getParentId());
					}
					if ("1".equals(dept.getDeptLevel())) {
						level = 0;
					} else {
						level = 1;
					}
				}
			} else {
				if ("2".equals(this.getDeptLevel())) {
					level = 2;
				} else if ("3".equals(this.getDeptLevel())) {
					level = 3;
				} else {
					Dept dept = this;
					while (!Dept.ROOT.equals(dept.getParentId())) {
						dept = this.getParentDept(dept.getParentId());
					}
					if ("2".equals(dept.getDeptLevel())) {
						level = 2;
					} else {
						level = 3;
					}
				}
			}
		}
		return level;
	}

	private Dept getParentDept(String parentId) {
		IDeptService ds = ContextHolder.getBean(IDeptService.BEAN_ID);
		return (Dept) ds.loadDeptById(parentId);
	}

	/**
	 * 获取该部门所属的处室
	 * 
	 * @return
	 */
	public IDept getCDept() {
		IDeptService ds = ContextHolder.getBean(IDeptService.BEAN_ID);
		if (this.getLevel() == 0) {
			if (this.getId().length() == 12) {
				return this;
			} else {
				return (Dept) ds.loadDeptById(StringUtils.substring(this.getId(), 0, 12));
			}
		} else {
			return this;
		}
	}

}
